var selectedFunction;
var response;
var entity;
var http;
var httpUrl;

function init() {
	http = new Http();
	
	_initHeader();
	_buttonHandler();
	
	if(memoryPreference.contains("httpUrl") == true)
		httpUrl = memoryPreference.get("httpUrl");
	else
		httpUrl = "http://203.235.211.98:7000/test/services/nmp";
	
	selectedFunction = "get";
}

function _buttonHandler() {
	$("#getButton").click(function(e) {
		selectedFunction = "get";
	});
	$("#postButton").click(function(e) {
		selectedFunction = "post";
	});
	$("#postBodyButton").click(function(e) {
		selectedFunction = "postBody";
	});
	$("#requestButton").click(requestStart);
}

function successCallback(httpResult) {
	platformUIComponent.dismissProgressDialog();
	notifySuccess("request (" + selectedFunction.toUpperCase() + " 방식)");
	
	alert(httpResult.response);
}

function errorCallback(http) {
	platformUIComponent.dismissProgressDialog();
	notifyError("request", http.errorMessage);
}

/*
 * GET 방식
 */
function get() {
	alert("GET 방식 통신을 시작합니다.");
	
	http.setRequestHeader("service", "login");
	http.setRequestHeader("app_id", "doollyGet");
	http.setRequestHeader("id", "test");
	http.setRequestHeader("pw", "test");
	http.setRequestHeader("timeout", "5000");
	
	entity = {
		"url" : httpUrl,
		"method" : "GET",
		"onBody" : false
	};
	
	return true;
}

/*
 * POST
 */
function post() {
	alert("POST 방식 통신을 시작합니다.");
	http.setRequestHeader("service", "login");
	http.setRequestHeader("app_id", "doollyPost");
	http.setRequestHeader("id", "test");
	http.setRequestHeader("pw", "test");
	http.setRequestHeader("timeout", "5000");
	
	entity = {
		"url" : httpUrl,
		"method" : "POST",
		"onBody" : false,
		"parameters" : {
			"parameter1" : "value1",
			"parameter2" : "value2"
		}
	};
}

/*
 * POSTBODY
 */
function postbody() {
	alert("POSTBODY 방식 통신을 시작합니다.");
	
	http.setRequestHeader("service", "login");
	http.setRequestHeader("app_id", "doollyPostbody");
	
	var content = {
		"id" : "test",
		"pw" : "test",
		"timeout" : "5000"
	};
	
	entity = {
		"url" : httpUrl,
		"method" : "POST",
		"onBody" : true,
		"content" : JSON.stringify(content)
	};
	
	return true;
}

/*
 * requestStart
 */
function requestStart() {
	var timeout = $("#timeoutInput").val() * 1000;
	
	if(timeout !== "" || timeout !== null || timeout !== undefined)
		http.setTimeout(timeout);
	
	if(selectedFunction == "get") {
		var result = get();
		if(!result)
			return;
	} else if(selectedFunction == "post") {
		post();
	} else {
		var result = postbody();
		if(!result)
			return;
	}
	
	commonProgress("Connecting..", true, "requestCancelCallback", "white");
	
	http.request(entity, successCallback, errorCallback);
}

function requestCancelCallback() {
	platformUIComponent.dismissProgressDialog();
	http.cancelRequest();
	
	notifySuccess("cancelRequest");
}